<?php

namespace App\Package\Attendance;

use App\Models\Schedule;

class Time
{
	/**
	 * 检查考勤状态
	 *
	 * Date: 30/04/2018
	 * @author George
	 * @param Schedule $schedule
	 * @param string $time
	 * @param string $type
	 * @return \Illuminate\Config\Repository|mixed
	 */
	public static function check(Schedule $schedule, string $time, string $type)
	{
		$delayed = config('attendance.delayed') * 60;
		$time = strtotime($time);

		if ($type === 'checkin') {
			$reference = strtotime($schedule->start_timestamp);
			if ($reference < ($time - $delayed)) {
				return config('attendance.status.tardy');
			}
		}

		if ($type === 'checkout') {
			$reference = strtotime($schedule->end_timestamp);
			if ($reference > $time) {
				return config('attendance.status.early');
			}
		}

		return config('attendance.status.normal');
	}

	/**
	 * 时间节点生成器
	 *
	 * Date: 02/05/2018
	 * @author George
	 * @param int $step 步长
	 * @param string $start 开始时间
	 * @param string $end 结束时间
	 * @return array
	 */
	public static function nodes(int $step = 30, string $start = '00:00', string $end = '23:59')
	{
		$times = [];
		$start = strtotime(date("Y-m-d {$start}:00"));
		$end = strtotime(date("Y-m-d {$end}:59"));
		while ($start < $end) {
			$start = strtotime(date('Y-m-d H:i:s', $start) . "+ {$step}minute");
			$times[] = date('H:i', $start);
		}
		return $times;
	}
}
